﻿using Coldairarrow.Entity.DingReving;
using Coldairarrow.Entity.PB;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using Coldairarrow.Entity.Print;
using System;
using Coldairarrow.IBusiness;

namespace Coldairarrow.Business.PB
{
    public partial class PB_CallPackListBusiness : BaseBusiness<PB_CallPackList>, IPB_CallPackListBusiness, ITransientDependency
    {
        readonly IOperator _operator;
        public PB_CallPackListBusiness(IDbAccessor db, IOperator @operator)
            : base(db)
        {
            _operator = @operator;

        }

        #region 外部接口

        public async Task<PageResult<PB_CallPackList>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<PB_CallPackList>();
            var search = input.Search;

            //筛选
            if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<PB_CallPackList, bool>(
                    ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
                where = where.And(newWhere);
            }

            return await q.Where(where).GetPageResultAsync(input);
        }

        public async Task<PB_CallPackList> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

      
        public async Task AddDataAsync(PB_CallPackList data)
        {
            await InsertAsync(data);
        }

        public async Task UpdateDataAsync(PB_CallPackList data)
        {
            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        public async Task AddCallPackList(List<AddCallPackList> addCallPackLists)
        {
            List<PB_CallPackList> CallPackList = new List<PB_CallPackList>();
            for (int i=0;i< addCallPackLists.Count;i++) {
                PB_CallPackList pbdata = new PB_CallPackList();
              
                string dataId = IdHelper.GetId();
                pbdata.Id = dataId;
                pbdata.PackageName = addCallPackLists[i].PackageName;
                pbdata.QrCode = h_Pack + "," + dataId;
                pbdata.PackageCode = addCallPackLists[i].PackageCode;
                pbdata.CreatorId = _operator.UserId;
                pbdata.CreateTime = DateTime.Now;
                pbdata.MaterialId = addCallPackLists[i].MaterialId;
                pbdata.Nums = addCallPackLists[i].Nums;
                CallPackList.Add(pbdata);
            }
            //插入数据
            await Db.InsertAsync<PB_CallPackList>(CallPackList);
        }

        public async Task<PB_Material> GetMaterial(string QrCode)
        {
            string qrcodekey = QrCode.Split(',')[0].ToString();
            string Materailid = string.Empty;
            if (qrcodekey == "04") {
                var model = QrCode.Split(',');
                if (model.Length > 2)
                {
                    Materailid = model?[3].ToString();
                }
                else
                {
                    Materailid = model?[1].ToString();
                }
                //合并重复物料指向新的物料Id
                PB_Material materdata = await Db.GetIQueryable<PB_Material>().Where(x => x.Id == Materailid).FirstOrDefaultAsync();
                if (!materdata.TransferId.IsNullOrEmpty())
                {
                    Materailid = materdata.TransferId;
                }
                var Mater = await Db.GetIQueryable<PB_Material>().Where(m => m.Id == Materailid).FirstOrDefaultAsync();
                PB_Material pB_Material = new PB_Material();
                pB_Material = Mater;
                return pB_Material;
            }
            else if (qrcodekey=="14") {
                string jobid = QrCode.Split(',')?[1].ToString();
                var pb_jobking =await Db.GetIQueryable<PB_JobBookingQrCode>().Where(m=>m.Id== jobid).FirstOrDefaultAsync();
                //合并重复物料指向新的物料Id
                PB_Material materdata = await Db.GetIQueryable<PB_Material>().Where(x => x.Id == pb_jobking.MaterialId).FirstOrDefaultAsync();
                if (!materdata.TransferId.IsNullOrEmpty())
                {
                    Materailid = materdata.TransferId;
                }
                var Mater = await Db.GetIQueryable<PB_Material>().Where(m => m.Id == Materailid).FirstOrDefaultAsync();
                PB_Material pB_Material = new PB_Material();
                pB_Material = Mater;
                return pB_Material;
            }

            return null;
        }

        #endregion

        #region 私有成员

        #endregion
    }
}